from tool.data_processing import DataProcessing


class RecvStructure(object):

    def __init__(self, data: str):
        data = data.upper()
        data = DataProcessing.str_to_bits(data)

        self.content_attribute = self.get_content_struct(data)

        self.message_head = self.content_attribute["协议头"]
        self.devices = self.content_attribute["模块"]
        self.Command = self.content_attribute["命令码"]
        self.Length = self.content_attribute["长度"]
        self.Signal = self.content_attribute["标志位"]
        self.Total = self.content_attribute["总包数"]
        self.Position = self.content_attribute["当前包"]
        self.data = self.content_attribute["数据域"]
        self.check = self.content_attribute["校验位"]

    @staticmethod
    def get_content_struct(socket_datas: list) -> dict:  # 协议结构
        data_size = DataProcessing.hexs_to_int(socket_datas[4:6]) - 9
        print(data_size)
        content_struct_dict = {
            "协议头": socket_datas[0:2],
            "模块": socket_datas[2],
            "命令码": socket_datas[3],
            "长度": data_size,
            "标志位": socket_datas[6],
            "总包数": socket_datas[7:9],
            "当前包": socket_datas[9:11],
            "数据域": socket_datas[11:11 + data_size],
            "校验位": socket_datas[11 + data_size:12 + data_size]
        }

        return content_struct_dict

    def get_content_attribute(self) -> dict:
        return self.content_attribute


if __name__ == '__main__':
    st = "aa330101001603000100010200303132333435363738393026"
    s = RecvStructure(st)
    a = s.get_content_attribute()
    for key, value in a.items():
        print(f"{key}： {value}")
